package com.yuexiao.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuexiao.config.RedisUtil;
import com.yuexiao.pojo.Category;
import com.yuexiao.pojo.Dish;
import com.yuexiao.pojo.Setmeal;
import com.yuexiao.service.CategoryService;
import com.yuexiao.mapper.CategoryMapper;
import com.yuexiao.service.DishService;
import com.yuexiao.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
 * @createDate 2022-08-10 14:10:57
 */
@Component
@Service(interfaceClass = CategoryService.class)
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    /**
     * 查询所有分类数据
     * @return List<Category> 分类列表
     */
    @Override
    public List<Category> getAllCategory() {
        return baseMapper.selectList(null);
    }

    /**
     * 分页查询类别列表
     * @param page int 当前页
     * @param pageSize int 每页显示条数
     * @return Page mybatisplus自带的分页对象
     */
    @Override
    public Page getCategoryByPage(int page, int pageSize) {

        //首先去redis中进行查询如果存在直接取出返回

        Page categorys= (Page) redisUtil.hget("category",page+"_"+pageSize);
        if(categorys!=null){
            return categorys;
        }

        //redis中不存在则去数据库查询
        Page pageQuery = new Page(page, pageSize);
        Page page1 = baseMapper.selectPage(pageQuery, null);
        //如果查询的结果不为空，且条数不为0则存储到redis
        if(page1!=null&&page1.getTotal()!=0){
            redisUtil.hset("category",page+"_"+pageSize,page1);
        }
        return page1;
    }

    /**
     * 新增类目信息
     * @param category Category 类目对象
     * @return boolean true or false
     */
    @Override
    public boolean insertCategory(Category category) {
        int insert = baseMapper.insert(category);
        if(insert>0){
            redisUtil.del("category");
            return true;
        }
        return false;
    }


    /**
     * 根据id删除类目数据
     * @param id Long 类目主键id
     * @return boolean true or false
     */
    @Override
    public boolean deleteCategory(Long id){
        //根据分类id查询菜品数据，如果有关联菜品则不允许删除
        LambdaQueryWrapper<Dish> dishQueryWrapper=new LambdaQueryWrapper();
        dishQueryWrapper.eq(Dish::getCategoryId,id);
        long dishCount = dishService.count(dishQueryWrapper);
        //如果查询结果大于0则代表有关菜品
        if(dishCount>0){
            throw new RuntimeException("have association");
        }

        //根据分类id查询套餐数据，如果有套餐则不允许删除
        LambdaQueryWrapper<Setmeal> setmealWrapper=new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId,id);
        long setmealCount = setmealService.count(setmealWrapper);
        //如果查询结果大于0则代表有套餐
        if(setmealCount>0){
            throw  new RuntimeException("have association");
        }

        //删除操作
        int result = baseMapper.deleteById(id);
        if(result>0){
            //如果删除成功，则清除redis
            redisUtil.del("category");
            return true;
        }
        return false;
    }

    /**
     * 更新分类信息
     * @param category Category 类目对象
     * @return boolean true or false
     */
    @Override
    public boolean updateCategory(Category category) {
        int result = baseMapper.updateById(category);
        if(result>0){
            //更新成功则删除redis中索引
            redisUtil.del("category");
            return true;
        }
        return false;
    }

    /**
     * 根据类型查询分类列表
     * @param type int 分类id
     * @return List<Category> 列表集合
     */
    @Override
    public List<Category> getCategoryListByType(int type) {
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Category::getType,type);
        return baseMapper.selectList(queryWrapper);
    }
}




